package org.broadinstitute.hellbender.tools.walkers.bqsr;


import org.broadinstitute.hellbender.CommandLineProgramTest;
import org.broadinstitute.hellbender.testutils.ArgumentsBuilder;
import org.broadinstitute.hellbender.GATKBaseTest;
import org.broadinstitute.hellbender.testutils.IntegrationTestSpec;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.File;

public final class GatherBQSRReportsIntegrationTest extends CommandLineProgramTest {

    private static final String testDir = GATKBaseTest.publicTestDir + "/org/broadinstitute/hellbender/utils/recalibration/";

    @Test
    public void testCombine5Reports() throws Exception {
        final File recal1 = new File(testDir + "HiSeq.1mb.1RG.sg1.table");
        final File recal2 = new File(testDir + "HiSeq.1mb.1RG.sg2.table");
        final File recal3 = new File(testDir + "HiSeq.1mb.1RG.sg3.table");
        final File recal4 = new File(testDir + "HiSeq.1mb.1RG.sg4.table");
        final File recal5 = new File(testDir + "HiSeq.1mb.1RG.sg5.table");

        //Note: the expected output was created by running GATK3, fixing table headers and sorting in unix "sort -k 2,2 -k 3,3 -k 4,4"
        final File recal_original = new File(testDir + "HiSeq.1mb.1RG.noSG.table");

        final ArgumentsBuilder args = new ArgumentsBuilder();
        args.addRaw("--input");
        args.addRaw(recal1.getAbsolutePath());
        args.addRaw("--input");
        args.addRaw(recal2.getAbsolutePath());
        args.addRaw("--input");
        args.addRaw(recal3.getAbsolutePath());
        args.addRaw("--input");
        args.addRaw(recal4.getAbsolutePath());
        args.addRaw("--input");
        args.addRaw(recal5.getAbsolutePath());

        final File outFile = GATKBaseTest.createTempFile("bqsr.", "table");
        args.addRaw("-O");
        args.addRaw(outFile.getAbsolutePath());
        final Object res = this.runCommandLine(args.getArgsArray());
        Assert.assertEquals(res, 0);
        IntegrationTestSpec.assertEqualTextFiles(outFile, recal_original);
    }

    @Test
    public void testCombineReportsFromTwoChrs() throws Exception {
        //NOte: this data was generated by running GATK BaseRecalibrator like this
        //  gatk BaseRecalibrator -I src/test/resources/large/CEUTrio.HiSeq.WGS.b37.NA12878.20.21.bam -O gatk4_chr20.tbl --knownSites src/test/resources/large/dbsnp_138.b37.20.21.vcf -R src/test/resources/large/human_g1k_v37.20.21.fasta -L 20
        //  gatk BaseRecalibrator -I src/test/resources/large/CEUTrio.HiSeq.WGS.b37.NA12878.20.21.bam -O gatk4_chr21.tbl --knownSites src/test/resources/large/dbsnp_138.b37.20.21.vcf -R src/test/resources/large/human_g1k_v37.20.21.fasta -L 21
        //  gatk GatherBQSRReports -I gatk4_chr20.tbl -I gatk4_chr21.tbl -O gatk4_combined.tbl
        // Then, the results were manually compared to those generated by GATK3.4 and confirmed to be the same up to 0.01 (which is OK)

        final File chr20 = new File(testDir + "gatk4_chr20.tbl.gz");
        final File chr21 = new File(testDir + "gatk4_chr21.tbl.gz");
        final File chr2021_combined = new File(testDir + "gatk4_combined.tbl.gz");

        final ArgumentsBuilder args = new ArgumentsBuilder();
        args.addRaw("--input");
        args.addRaw(chr20.getAbsolutePath());
        args.addRaw("--input");
        args.addRaw(chr21.getAbsolutePath());

        final File outFile = GATKBaseTest.createTempFile("bqsr2021.", "table");
        args.addRaw("-O");
        args.addRaw(outFile.getAbsolutePath());
        final Object res = this.runCommandLine(args.getArgsArray());
        Assert.assertEquals(res, 0);
        IntegrationTestSpec.assertEqualTextFiles(outFile, chr2021_combined);
    }
}
